<template>
  <van-row>
    <van-nav-bar
      :title="title"
      left-arrow
      @click-left="$emit('close')"
    />
    <van-form @submit="doSubmit">
      <van-field
        v-model="submitForm.code"
        name="工序编码"
        label="工序编码"
        placeholder="(必填)工序编码"
        :rules="[{ required: true, message: '请填写工序编码' }]"
        input-align="left"
      />
      <van-field
        v-model="submitForm.name"
        name="工序名称"
        label="工序名称"
        placeholder="(必填)工序名称"
        :rules="[{ required: true, message: '请填写工序名称' }]"
        input-align="left"
      />
      <van-field
        v-model="submitForm.unitPrice"
        type="number"
        label="单价"
        placeholder="请输入工序单价"
        input-align="left"
        :rules="[{ required: true, message: '请输入工序单价' }]"
      />
      <van-field
        v-model="submitForm.remark"
        rows="2"
        autosize
        label="备注"
        type="textarea"
        maxlength="100"
        show-word-limit
        placeholder="(非必填)备注"
        input-align="left"
      />
      <div style="margin: 16px;">
        <van-button round block type="danger" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </van-row>
</template>

<script>
import { updateProcessById, selectProcessById, saveProcess } from '@/api/process'

export default {
  name: 'ProcessSubmitModule',
  data() {
    return {
      title: '',
      submitForm: {}
    }
  },
  methods: {
    init(id) {
      this.resetForm()
      this.title = '修改工序'
      if (!id) {
        this.title = '新增工序'
        return
      }
      selectProcessById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      })
    },
    resetForm() {
      this.submitForm = {
        id: '',
        name: '',
        code: '',
        state: '1'
      }
    },
    doSubmit() {
      let request = saveProcess
      if (this.submitForm.id) {
        request = updateProcessById
      }
      request(this.submitForm).then(resp => {
        if (resp.success) {
          this.$toast.success('操作成功')
          this.$emit('close')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
